package com.open.party.cloud.view.home.jiCengDangJian.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.open.party.cloud.R;
import com.open.party.cloud.model.TimeBean;
import com.sinothk.android.utils.XUtils;
import com.sinothk.view.image.rounded.RoundedImageView;

import java.util.ArrayList;
import java.util.List;

import com.sinothk.android.utils.inters.OnSuperListener;

public class TimeListAdapter extends RecyclerView.Adapter<TimeListAdapter.ViewHolder> {

    // 展示数据
    private ArrayList<TimeBean> mData;
    private Context mContext;
    private OnSuperListener<TimeBean> onItemClickListener;

    public TimeListAdapter(Context mContext) {
        this.mContext = mContext;
        this.mData = new ArrayList<>();
    }

    public void setOnSuperListener(OnSuperListener<TimeBean> onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    public void setData(List<TimeBean> data) {
        if (mData == null) {
            mData = new ArrayList<>();
        } else {
            mData.clear();
        }
        if (data == null) {
            data = new ArrayList<>();
        }
        this.mData.addAll(data);
        notifyDataSetChanged();
    }

    public void updateData(List<TimeBean> data) {
        this.mData.addAll(data);
        notifyDataSetChanged();
    }

    @Override
    public int getItemCount() {
        return mData == null ? 0 : mData.size();
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
        View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.time_list_item, viewGroup, false);
        return new ViewHolder(view);
    }

    @SuppressLint("SetTextI18n")
    @Override
    public void onBindViewHolder(ViewHolder holder, final int position) {
        // 绑定数据
        final TimeBean itemData = mData.get(position);

//        holder.titleTv.setText(XUtils.string().getNotNullValue(itemData.getContent()));

        String sTimeStr = itemData.getServiceStartTime() != null ? XUtils.date().getDateStrByDate(itemData.getServiceStartTime(), "yyyy年MM月dd日 HH:mm") : "未知";
        String eTimeStr = itemData.getServiceEndTime() != null ? XUtils.date().getDateStrByDate(itemData.getServiceEndTime(), "HH:mm") : "未知";
        holder.timeTv.setText(sTimeStr + " - " + eTimeStr);

        // 活动类型
//        String planTypeStr = "";
//        switch (itemData.getPlanType()) {
//            // 活动类型: 0.签到、1.活动，2. 餐饮、3.议程
//            case 0:
//                planTypeStr = "签到";
//                break;
//            case 1:
//                planTypeStr = "活动";
//                break;
//            case 2:
//                planTypeStr = "餐饮";
//                break;
//            case 3:
//                planTypeStr = "议程";
//                break;
//        }
//        holder.planTypeTv.setText(planTypeStr);

        if (onItemClickListener != null) {

            holder.rootView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    onItemClickListener.onClick(position, itemData, "");
                }
            });
        }
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        LinearLayout rootView;
        TextView titleTv, planTypeTv, timeTv;
        RoundedImageView imageView;

        ViewHolder(View itemView) {
            super(itemView);
            rootView = itemView.findViewById(R.id.rootView);

            titleTv = itemView.findViewById(R.id.titleTv);
            timeTv = itemView.findViewById(R.id.timeTv);
            planTypeTv = itemView.findViewById(R.id.planTypeTv);
        }
    }
}